package com.yc.boot.work.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.boot.common.model.ExcelAlias;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.common.utils.ExcelUtil;
import com.yc.boot.work.business.TestClassBussiness;
import com.yc.boot.work.business.TestClassTeacherBussiness;
import com.yc.boot.work.entity.Test;
import com.yc.boot.work.entity.TestClass;
import com.yc.boot.work.model.TestClassAndTeacherPart;
import com.yc.boot.work.model.TestClassInfo;
import com.yc.boot.work.model.TestClassStudent;
import com.yc.boot.work.model.TestClassTeacherNode;
import com.yc.boot.work.model.find.FindClassPage;
import com.yc.boot.work.model.find.TestPage;
import com.yc.boot.work.model.save.SaveClass;
import com.yc.boot.work.model.update.UpdateClass;
import com.yc.boot.work.service.TestClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2021/12/14/13:56
 * @Description:  班级controller层
 */
@RestController
@Api(tags = "测试班级接口")
@RequestMapping("/class")
public class TestClassApi {
    @Resource
     private TestClassBussiness testClassBussiness;
    @Resource
    private TestClassTeacherBussiness testClassTeacherBussiness;

    @Resource
    private TestClassService testClassService;

    @GetMapping("/getClassByName/{name}")
    @ApiOperation("根据名字获取班级信息")
    public Outcome<TestClass> queryClassByname(@PathVariable  String name){
        return Outcome.success(testClassBussiness.queryByNameOcc(name));
    }

    @PostMapping("/saveClassOne/")
    @ApiOperation("添加班级单条信息")
    public Outcome<TestClass> saveClassOne(@RequestBody SaveClass param){
        return Outcome.status(testClassBussiness.addClassOne(param));
    }
    @GetMapping("/queryClassPage/")
    @ApiOperation("分页查询班级信息")
    public Outcome<IPage<TestClass>> queryClassPage(FindClassPage param){
        return Outcome.success(testClassBussiness.queryClass(param));
    }
    @GetMapping("/getClassInfo/")
    @ApiOperation("获取班级详细信息")
    public Outcome<List<TestClassInfo>> getClassInfo(){
        return Outcome.success(testClassTeacherBussiness.getClassInfo());
    }

    @GetMapping("/getClassAndTeacherPart/")
    @ApiOperation("测试select()获取班级教师部分信息")
    public Outcome<List<TestClassInfo>> getClassAndTeacherPart(){
        return Outcome.success(testClassTeacherBussiness.getClassAndTeacher());
    }

    @GetMapping("/getPartOfClassAndTeacher/")
    @ApiOperation("获取班级教师部分信息")
    public Outcome<List<TestClassAndTeacherPart>> getPartOfClassAndTeacher(){
        return Outcome.success(testClassTeacherBussiness.getPartOfClassAndTeacher());
    }

    @GetMapping("/queryPartOfClassAndTeacher/")
    @ApiOperation("用注解方式获取班级教师部分信息")
    public Outcome<List<TestClassTeacherNode>> queryPartOfClassAndTeacher(){
        return Outcome.success(testClassTeacherBussiness.queryPartOfClassAndTeachers());
    }

    @GetMapping("/queryClassAndStudent/")
    @ApiOperation("一对多获取班级和学生全部信息")
    public Outcome<List<TestClassStudent>> queryClassAndStudent(){
        return Outcome.success(testClassBussiness.queryClassAndStudent());
    }

    @PutMapping("/updateClass/")
    @ApiOperation("修改班级信息")
    public Outcome updateClass(@RequestBody @Valid UpdateClass classinfo){
        return testClassBussiness.updateClass(classinfo);

    }

    @DeleteMapping("/delClass")
    @ApiOperation("删除班级信息")
    public Outcome delClass(@RequestBody @Valid @Size(min = 1) List<Long> id){
        return  Outcome.status(testClassBussiness.delClass(id));
    }


    @GetMapping("/downExcl")
    @ApiOperation(value = "下载excl表格", produces = "application/octet-stream")
    public void downExcl(FindClassPage param) {
        IPage<TestClass> page = testClassBussiness.queryClass(param);
        ExcelUtil.downExcel(page.getRecords(),
                CollUtil.newArrayList(ExcelAlias.makeAlias("id", "主键"),
                        ExcelAlias.makeAlias("createBy", "创建人"),
                        ExcelAlias.makeAlias("createTime", "创建时间"),
                        ExcelAlias.makeAlias("updateBy", "修改人"),
                        ExcelAlias.makeAlias("updateTime", "修改时间"),
                        ExcelAlias.makeAlias("isDelete", "是否删除"),
                        ExcelAlias.makeAlias("name", "班级名称"),
                        ExcelAlias.makeAlias("number", "班级人数")));
    }
}
